﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Services
{
	using System;
	using System.Collections.Generic;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Extensions;
	using Falcon.Forums.Domain;
	using Falcon.Forums.Domain.Repositories;
	using Falcon.Forums.Domain.Services;
	using Falcon.Forums.Domain.Values;

	public class ForumAnnouncementService : IForumAnnouncementService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ForumAnnouncementService"/> class.
		/// </summary>
		/// <param name="identityService">The identity service.</param>
		/// <param name="forumAnnouncementRepository">The forum announcement repository.</param>
		public ForumAnnouncementService(
			IIdentityService identityService, 
			IForumAnnouncementRepository forumAnnouncementRepository)
		{
			this.IdentityService = identityService;
			this.ForumAnnouncementRepository = forumAnnouncementRepository;
		}

		/// <summary>
		/// Gets or sets the identity service.
		/// </summary>
		/// <value>The identity service.</value>
		public IIdentityService IdentityService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the forum announcement repository.
		/// </summary>
		/// <value>The forum announcement repository.</value>
		public IForumAnnouncementRepository ForumAnnouncementRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the forum announcement by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public ForumAnnouncement GetById(int id)
		{
			return this.ForumAnnouncementRepository.GetById(id);
		}

		/// <summary>
		/// Gets all the forum announcements.
		/// </summary>
		/// <returns></returns>
		public IList<ForumAnnouncement> GetAll()
		{
			return this.ForumAnnouncementRepository.GetAll();
		}

		/// <summary>
		/// Gets all the forum announcements by the specified specification.
		/// </summary>
		/// <param name="specification">The specification.</param>
		/// <returns></returns>
		public IList<ForumAnnouncement> GetAll(ForumAnnouncementSpecification specification)
		{
			return this.ForumAnnouncementRepository.GetAll(specification);
		}

		/// <summary>
		/// Creates the forum announcement.
		/// </summary>
		/// <returns></returns>
		public ForumAnnouncement Create()
		{
			var announcement = new ForumAnnouncement
			{
				Created = DateTime.Now
			};

			announcement.Updated = announcement.Created;

			return announcement;
		}

		/// <summary>
		/// Inserts the specified announcement.
		/// </summary>
		/// <param name="announcement">The announcement.</param>
		public void Insert(ForumAnnouncement announcement)
		{
			announcement.Validate();

			announcement.Slug = announcement.Title.Slug();
			announcement.BodyParsed = announcement.Body.BBCodeToHtml();

			this.ForumAnnouncementRepository.Insert(announcement);
		}

		/// <summary>
		/// Inserts the specified announcement.
		/// </summary>
		/// <param name="announcement">The announcement.</param>
		/// <param name="value">The value.</param>
		public void Insert(ForumAnnouncement announcement, ForumAnnouncementCreate value)
		{
			value.Validate();

			announcement.ForumId = value.ForumId;
			announcement.UserId = IdentityService.Current.Id;
			announcement.Title = value.Title;
			announcement.Body = value.Body;
			announcement.Start = DateTime.Parse(value.Start);
			announcement.Expire = DateTime.Parse(value.Expire);

			this.Insert(announcement);
		}

		/// <summary>
		/// Updates the specified announcement.
		/// </summary>
		/// <param name="announcement">The announcement.</param>
		public void Update(ForumAnnouncement announcement)
		{
			announcement.Validate();

			announcement.Slug = announcement.Title.Slug();
			announcement.BodyParsed = announcement.Body.BBCodeToHtml();

			this.ForumAnnouncementRepository.Update(announcement);
		}

		/// <summary>
		/// Updates the specified announcement.
		/// </summary>
		/// <param name="announcement">The announcement.</param>
		/// <param name="value">The value.</param>
		public void Update(ForumAnnouncement announcement, ForumAnnouncementUpdate value)
		{
			value.Validate();

			announcement.ForumId = value.ForumId;
			announcement.Title = value.Title;
			announcement.Body = value.Body;
			announcement.Start = DateTime.Parse(value.Start);
			announcement.Expire = DateTime.Parse(value.Expire);

			this.Update(announcement);
		}

		/// <summary>
		/// Deletes the specified announcement.
		/// </summary>
		/// <param name="announcement">The announcement.</param>
		public void Delete(ForumAnnouncement announcement)
		{
			this.ForumAnnouncementRepository.Delete(announcement);
		}
	}
}